package me.shurufa.activities;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import butterknife.Bind;
import cn.finalteam.okhttpfinal.HttpRequest;
import de.greenrobot.event.EventBus;
import me.shurufa.R;
import me.shurufa.bean.BookComment;
import me.shurufa.bean.UserInfoEntity;
import me.shurufa.db.PersistenceUtils;
import me.shurufa.net.API;
import me.shurufa.net.RequestParams;
import me.shurufa.net.okhttp.callback.HttpCallback;
import me.shurufa.response.CreateBookCommentResp;
import me.shurufa.utils.Constants;
import me.shurufa.utils.Utils;

/**
 * 作者 : andy
 * 日期 : 16/3/24 09:23
 * 邮箱 : andyxialm@gmail.com
 * 描述 : 发送书评
 */
public class SendBookCommentActivity extends BaseActivity implements View.OnClickListener {
    @Bind(R.id.right_text)
    TextView rightText;
    @Bind(R.id.et_content)
    EditText et_content;

    private long mBookId;
    private String mBookName;
    private Handler mHandler = new Handler();


    @Override
    public void onCreate(Bundle savedInstanceState) {
        activity_layout_res = R.layout.activity_send_book_comment;
        super.onCreate(savedInstanceState);
        parseArguments();
        initUI();
    }

    private void initUI() {
        titleTextView.setText(mBookName);
        rightText.setVisibility(View.VISIBLE);
        rightText.setText(getString(R.string.comment));
        rightText.setOnClickListener(this);

        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                Utils.showSoftkeyboard(SendBookCommentActivity.this, et_content);
            }
        }, 400);
    }

    public static void initArguments(Intent in, long bookId, String bookName) {
        in.putExtra(Constants.ARG_BOOK_ID, bookId);
        in.putExtra(Constants.ARG_BOOK_TITLE, bookName);
    }

    private void parseArguments() {
        if (getIntent() != null) {
            mBookId = getIntent().getLongExtra(Constants.ARG_BOOK_ID, 0l);
            mBookName = getIntent().getStringExtra(Constants.ARG_BOOK_TITLE);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.right_text:

                // 隐藏输入法
                Utils.closeInputMethod(this, et_content);

                final String content = et_content.getText().toString().trim();
                if (!TextUtils.isEmpty(content) && content.length() > 6 && content.length() < 3000) {

                    reqCreateComment(content);

                } else if (TextUtils.isEmpty(content)) {
                    Utils.showToast(R.string.content_is_empty);
                } else if (content.length() < 10){
                    Utils.showToast("内容不可少于10字");
                } else if (content.length() > 3000) {
                    Utils.showToast("内容不可超过3000字");
                } else {
                    Utils.showToast("内容字数限制10-3000字");
                }
                break;
        }
    }

    /**
     * 创建书评
     * @param content 书评内容
     */
    private void reqCreateComment(final String content) {
        RequestParams params = new RequestParams();
        params.addFormDataPart("id", mBookId);
        params.addFormDataPart("type", "book");
        params.addFormDataPart("content", content);

        HttpRequest.post(API.CREATE_BOOK_COMMENT, params, new HttpCallback<CreateBookCommentResp>(this) {
            @Override
            public void onRespSuccess(CreateBookCommentResp resp) {
                try {
                    Utils.showToast(getString(R.string.success_add_book_comment));
                    et_content.setText("");

                    BookComment newBookComment = new BookComment();
                    newBookComment.id = resp.id;
                    newBookComment.book_id = (int) mBookId;
                    newBookComment.content = content;
                    newBookComment.created_at = System.currentTimeMillis() / 1000;

                    UserInfoEntity userInfoEntity = new UserInfoEntity();
                    userInfoEntity.id = Long.parseLong(PersistenceUtils.getUserId());
                    userInfoEntity.avatar = PersistenceUtils.getAvatar();
                    userInfoEntity.username = PersistenceUtils.getUsername();
                    newBookComment.user_info = userInfoEntity;

                    Intent in = new Intent(Constants.EVENT_SEND_BOOK_COMMENT);
                    in.putExtra(Constants.EVENT_SEND_BOOK_COMMENT, newBookComment);
                    in.putExtra(Constants.ARG_BOOK_ID, (int) mBookId);
                    EventBus.getDefault().post(in);

                    mHandler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            finish();
                        }
                    }, 100);
                } catch (Exception e) {
                }
            }

            @Override
            public void onRespFailure(CreateBookCommentResp resp) {

            }

        });
    }
}
